package cn.lingyangwl.agile.auth.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.lingyangwl.agile.auth.oauth2.model.OAuth2TokenResp;
import cn.lingyangwl.agile.auth.service.OAuth2ClientService;
import cn.lingyangwl.agile.auth.service.OAuth2GrantService;
import cn.lingyangwl.agile.auth.service.OAuth2TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author shenguangyang
 */
@Slf4j
@Service
public class OAuth2GrantServiceImpl implements OAuth2GrantService {
    @Resource
    private OAuth2ClientService clientService;
    @Resource
    private OAuth2TokenService tokenService;

    @Override
    public boolean revokeToken(String clientId, String accessToken) {
        // 先查询，保证 clientId 时匹配的
        OAuth2TokenResp tokenResp = tokenService.parseAccessToken(accessToken);
        if (tokenResp == null || ObjectUtil.notEqual(clientId, tokenResp.getClientId())) {
            log.warn("移除token失败, oAuth2Token: {}", tokenResp);
            return false;
        }

        return tokenService.removeAccessToken(accessToken);
    }
}
